<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/3/7
 * Time: 下午5:34
 */
class Admin_model extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
        $this->load->helper("common");
    }

    //修改admin账期信息
    public function save_admin_info($customer_id, $post)
    {
        $data['account_period_status'] = $post['account_period_status'];
        $bool = $this->db->where(array("customer_id" => $customer_id))->update('app_admin', $data);
        return $bool;
    }

    //获取admin的信息
    public function query_admin_info($customer_id)
    {
        $sql = "select * from app_admin WHERE customer_id = " . $customer_id;
        return $this->db->query($sql)->row_array();
    }


    public function save_admin_info_by_shop_customer_id($shop_customer_id, $post)
    {
        $sql = "select * from app_customer WHERE shop_user_id = " . $shop_customer_id;
        $r_info = $this->db->query($sql)->row_array();
        if ($r_info) {
            // 更新 ?

            return $r_info['id'];
        } else {

            $data['username'] = '';
            if (isset($post['username']) && $post['username']) {
                $data['username'] = $post['username'];
            } else {
                $url = $this->config->item("shop_url") . 'companymanagerapi/company_user_info/';
                $data_arr['time'] = time();
                $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
                $data_arr['shop_customer_id'] = $shop_customer_id;
                $rs = curl_request($url, $data_arr);
                $rs = json_decode($rs, true);
                if ($rs['code'] == '200') {
                    $data['username'] = $rs['data']['user_name'];
                }
            }

            // 插入
            //$data['username'] = isset($post['username']) ? $post['username'] : '';
            $data['shop_user_id'] = $shop_customer_id;
            $data['manager_id'] = isset($post['manager_id']) ? $post['manager_id'] : '';
            $data['company'] = isset($post['company']) ? $post['company'] : '';
            $data['name'] = isset($post['customer_name']) ? $post['customer_name'] : '';
            $data['tel'] = isset($post['tel']) ? $post['tel'] : '';
            $data['email'] = isset($post['email']) ? $post['email'] : '';
            $data['discount'] = isset($post['discount']) ? $post['discount'] : 100;
            $data['adjustments'] = isset($post['adjustments']) ? $post['adjustments'] : 0;
            $data['created_at'] = date('Y-m-d H:i:s', time());
            $data['identity'] = 1;
            $this->db->insert('app_customer', $data);
            $insert_id = $this->db->insert_id();
            // app_admin 表
            if ($insert_id) {
                $data2['customer_id'] = $insert_id;
                $data2['prefix'] = isset($post['username']) ? $post['username'] : '';
                $bool1 = $this->db->insert('app_admin', $data2);
            } else {
                return false;
            }
            if ($bool1) {
                return $insert_id;
            } else {
                return false;
            }
        }
    }


    public function save_customer_info_by_shop_customer_id($shop_customer_id, $post)
    {
        $sql = "select * from app_customer WHERE shop_user_id = " . $shop_customer_id;
        $r_info = $this->db->query($sql)->row_array();
        if ($r_info) {
            // 更新 ?
            return $r_info['id'];
        } else {
            // 插入
            $data['username'] = isset($post['user_name']) ? $post['user_name'] : '';
            $data['shop_user_id'] = $shop_customer_id;
            $data['name'] = isset($post['user_real_name']) ? $post['user_real_name'] : '';
            $data['tel'] = isset($post['user_phone_mob']) ? $post['user_phone_mob'] : '';
            $data['email'] = isset($post['user_email']) ? $post['user_email'] : '';
            $data['discount'] = 100;
            $data['adjustments'] = 0;
            $data['created_at'] = date('Y-m-d H:i:s', time());
            $data['identity'] = 1;
            $this->db->insert('app_customer', $data);
            $insert_id = $this->db->insert_id();
            // app_admin 表
            if ($insert_id) {
                $data2['customer_id'] = $insert_id;
                $data2['prefix'] = isset($post['user_name']) ? $post['user_name'] : '';
                $bool1 = $this->db->insert('app_admin', $data2);
            } else {
                return false;
            }
            if ($bool1) {
                return $insert_id;
            } else {
                return false;
            }
        }

    }

    public function change_prefix($customer_id, $prefix)
    {
        // 是否创建过 manager 或 user
        $sql = "select count(1) as res from app_customer WHERE parent_id = " . $customer_id;
        $res = $this->db->query($sql)->row_array()['res'];
        if ($res) {
            return ['code' => -1, 'msg' => '无法变更，已经创建过角色'];
        }
        $bool = $this->db->where(array("customer_id" => $customer_id))->update('app_admin', ["prefix" => $prefix]);
        if ($bool) {
            return ['code' => 0, 'msg' => "更新成功"];
        } else {
            return ['code' => -1, 'msg' => "更新失败"];
        }

    }


}